# -*- coding: utf-8 -*-
# Adapted by Christopher Steel of Voice of Access as automated admin user creater.

# Original liscense:

# Instant Press, site that work out the box. Blog system for Web2py Framework 
# Site: http://www.instant2press.com
# Site: http://code.google.com/p/instant-press/
# Copyright (c) 2010- Mulone, Pablo Martín (mulone.martin@gmail.com)
# License: GPL, General Public License v.2
# Visit: www.web2py.com or Groups: http://groups.google.com/group/web2py and http://groups.google.com/group/web2py-usuarios  

#ATTENTION!: do not use this method to add users, instead use
#registration, this is for testing purpose

def _fill_new_user(first_name,last_name,email,passw):    
    users = db(db.auth_user.email==email).select() #GAE
    if users:
        return users[0].id
    else:
        my_crypt = CRYPT(key=auth.settings.hmac_key)
        crypt_pass = my_crypt(passw)[0]        
        id_user= db.auth_user.insert(
                                   first_name=first_name,
                                   last_name=last_name,
                                   email = email,
                                   password = crypt_pass                             
                                   )
        return id_user

def _fill_require_admin_group():    
    group_admin = db(db.auth_group.role==DEFAULT_GROUPADMIN).select()
    if group_admin:
        return group_admin[0].id
    else:       
        id_group_admin = db.auth_group.insert(
                                    role=DEFAULT_GROUPADMIN,
                                    description = 'This is admin group'
                                    )
        return id_group_admin
    
def _is_no_user_in_db():        
    users_count = db(db.auth_user.id>0).count() #GAE
    if (users_count>0): 
        return False
    else:
        return True

def _fill_required_admin():
    id_group_admin = _fill_require_admin_group()    
    users_count = db(db.auth_user.id>0).count() #GAE
    if (users_count==0): #if no users
        id_user = _fill_new_user('John','Doe',DEFAULT_ADMIN_EMAIL,DEFAULT_ADMIN_PASSWORD)
        auth.add_membership(id_group_admin, id_user)  
        
    
def _fill_get_admin_guy():
    users = db(db.auth_user.email==DEFAULT_ADMIN_EMAIL).select() #GAE
    if users:
        return users[0].id
    else:
        return 0

def _fill_test_users():
    iduser = _fill_new_user('Chris','Mills','chris@nobody.com','somepasswordhere')
    iduser = _fill_new_user('Chris','Mills2','chris1@nobody.com','somepasswordhere')
    iduser = _fill_new_user('Chris','Mills3','chris2@nobody.com','somepasswordhere')
# coding: utf8
